package net.aaa.hotel.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "encodeFilter", value = "/beautyButt/*")
public class EncodeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
// 获得在下面代码中要用的request,response,session对象
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        HttpSession session = servletRequest.getSession();
// 获得用户请求的URI
        String path = servletRequest.getRequestURI();

// 从session里取员工工号信息
        String empId =(String) session.getAttribute("uname");

// 登陆页面无需过滤
       /* if (path.indexOf("/hlogin.jsp") > -1) {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }*/

// 判断如果没有取到员工信息,就跳转到登陆页面
        if ( empId == null || "".equals(empId)) {

// 跳转到登陆页面
            servletResponse.sendRedirect("/hlogin.jsp");
        } else {
// 已经登陆,继续此次请求
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
